import md5 from "js-md5"

export function convertObjToURI( paramsObj = {} ) {
  return Object.keys(paramsObj).map( (key) => (encodeURIComponent(key) + '=' + encodeURIComponent(paramsObj[key]))).join('&')
}

export function encryptionData(key, productId) {
	const encryptionData = key.slice(3)+productId+Math.round(new Date() / 1000)
	return md5(encryptionData)
}

export const registerParamsEvent = (callback, eventKey = 'paramsEvent') => {
  if (callback && callback instanceof Function) uni.$once(eventKey, callback)
}

export const getLocal = () => {
	const position= { code: '-1', data: '' }
	uni.getLocation({
	  geocode: true,
	  success: res => {
	    uni.chooseLocation({
	      latitude: res.latitude,
	      longitude: res.longitude,
	      success: result => {
          position = { data: result, code: 100 }
	      }
	    })
	  }
	})
	return position
}